<?php

/**
 * Controller welche von WSB_Controller_Action ableiteten, erben die
 * Standardmethoden listAction, editAction, deleteAciton, saveAction.
 * Dieser Standardcontroller arbeitet mit dem entsprechenden WSB_Model
 * zusammen.
 */
abstract class WSB_Controller_Action extends Zend_Controller_Action
{

	/**
	 * @var string
	 */
	protected $_listView = 'list.phtml';

	/**
	 * @var string
	 */
	protected $_editView = 'edit.phtml';

	/**
	 * @var string
	 */
	protected $_viewView = 'view.phtml';

	/**
	 * @var array
	 */
	protected $_models = array();

	/**
	 * Definiert, ob es sich um ein erweitertes Frontend-Modulen wie News,
	 * Shop, etc. handelt. Steuert das Verhalten von Javascript und Weiterleitungen.
	 *
	 * @var boolean
	 */
	protected $_isFrontendExtension = false;

	/**
	 * Initializer
	 * @return void
	 */
	public function init()
	{
		parent::init(); // immer zuerst!

		$instance_id = $this->_getParam('instance_id', 0);
		if($instance_id > 0) {
			$session = $this->getHelper('Session');
			$session->instance_id = $instance_id;
		}
	}

	/**
	 * Die Default LIST Action
	 *
	 */
    public function listAction()
	{

        $session = $this->getHelper('Session');

        // Hole Array $sessionPairs aus Sessionobjekt
		$sessionPairs = $session->models;

		$config = Zend_Registry::get('Zend_Config');
        $modelIndex = $this->_getParam('model', 0);

		// Sessionvariablen Initialisieren
        foreach($this->_models AS $key=>$model) {
            // Filter
            if(!isset($sessionPairs[$key]['filter'])) {
                $sessionPairs[$key]['filter'] = array();
            }
            // Offset
            if(!isset($sessionPairs[$key]['offset'])) {
                $sessionPairs[$key]['offset'] = 0;
            }
            // Order
            if(!isset($sessionPairs[$key]['order'])) {
                $prototype = new $model();
                $sessionPairs[$key]['order'] = $prototype->getOrder();
            }
        }


        // ------------------------------------------------------------------
		// Parameter fuer Filterung.
        $filter = $this->_getParam('filter', array());
        $filterModelIndex = isset($filter['model']) ? $filter['model'] : 0;
        if(key_exists('set', $filter)) {
            $sessionPairs[$filterModelIndex]['filter'] = $filter;
        }
        if(key_exists('reset', $filter)) {
            $sessionPairs[$filterModelIndex]['filter'] = array();
        }
		// ------------------------------------------------------------------


		// ------------------------------------------------------------------
    	// Parameter fuer Paginierung
		$count = $config->application->paginationCount;
		$offset = $this->_getParam('offset');
		if(is_numeric($offset)) {
			if($offset < 0) {
				$offset = 0;
			}
			$sessionPairs[$modelIndex]['offset'] = $offset;
		}
		// ------------------------------------------------------------------

		// ------------------------------------------------------------------
		// Einfaches Ordering
		if($this->_getParam('order') != '') {

			// In $this->_getParam('order') steht nur das Feld
			// In der Session wird aber auch die Sortierrichtung gespeichert, also z.B. "id ASC"
			$oldOrderParts = WSB_Div::csvToArray($sessionPairs[$modelIndex]['order'], ' ');
			$oldOrderField = $sessionPairs[$modelIndex]['order'];
			$oldOrderDirection = 'ASC';
			if(count($oldOrderParts) == 1) {
				$oldOrderField = $oldOrderParts[0];
				$oldOrderDirection = 'ASC';
			}
			if(count($oldOrderParts) >= 2) {
				$oldOrderField = $oldOrderParts[0];
				$oldOrderDirection = $oldOrderParts[1];
			}

			$newOrderField = $this->_getParam('order');
			$newOrderDirection = 'ASC';
			if($newOrderField == $oldOrderField) {
				if($oldOrderDirection == 'ASC') {
					$newOrderDirection = 'DESC';
				} elseif ($oldOrderDirection == 'DESC') {
					$newOrderDirection = 'ASC';
				}
			}

			$sessionPairs[$modelIndex]['order'] = $newOrderField . ' ' . $newOrderDirection;
		}
		// ------------------------------------------------------------------

		// Titel
		$this->view->titleType = 'h1';
		if(count($this->_models) > 1) {
            $this->view->titleType = 'h2';
		}

        foreach($this->_models AS $key=>$model) {

        	$order  = $sessionPairs[$key]['order'];
            $filter = $sessionPairs[$key]['filter'];
            $offset = $sessionPairs[$key]['offset'];
            $prototype = new $model();
            $this->view->modelIndex = $key;
            $this->view->prototype = $prototype;
    		$this->view->filter = $filter;
    		$this->view->order = $order;
    		$parts = explode(' ', $order);
    		$this->view->orderColumn = isset($parts[0]) ? $parts[0] : '';
    		$this->view->orderDirection = isset($parts[1]) ? $parts[1] : '';
    		$this->view->count = $count;
    		$this->view->offset = $offset;
    		$where = $this->_buildWhereStatement($filter);
    		$this->view->total = $this->_getCountAll($prototype, $where);
        	$this->view->collection = $prototype->fetchAll($where, $order, $count, $offset);
        	$this->view->controller = $this;
    		$this->render('list', null, true);
        }

        // Array $sessionPairs in Sessionobjekt speichern
		$session->models = $sessionPairs;

    }

    /**
     * Die Default VIEW Action
     *
     */
	public function viewAction()
	{
    	$table = $this->_getModelInstance();
    	$primary = $table->getPrimary();
    	$id = intval($this->_getParam($primary));
		if($id > 0) {
	    	$select = $table->select();
			$select->where($primary.'= ?', $this->_getParam($primary));
			$item = $table->fetchRow($select);

			$session = $this->getHelper('Session');

			// Redirect (siehe editAction)
			$redirect = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '' ;
			$redirect = $this->_getParam('redirect', $redirect);
			if(empty($redirect)) {
				$redirect = $session->redirect;
			} else {
				$session->redirect = $redirect;
			}

			// Javascript fuer onclick-Attribut in Button
			if(($this->_getParam('TEMPLATE') == 'popup') && (!$this->_isFrontendExtension)) {
				$onclickJs = 'window.close();';
			} else {
				$onclickJs = sprintf("javascript:window.location.href='%s';", $redirect);
			}

			$editor = $item->getFormEditor();
			$editor->setFormAction($_SERVER['REQUEST_URI']);
			$editor->addButton(WSB_BTN_CANCEL, array('label' => 'button:cancel', 'onclick' => $onclickJs));
			$editor->setRedirect($redirect);
			$form = $editor->getForm();
			$this->view->form = $form;
			$this->view->tablename = $item->getTableName();
			$this->render('view', null, true);

	    	// Page Tree rendern
	    	$moduleName = $this->getRequest()->getModuleName();
	    	if((substr($moduleName, 0, 2) == 'xt') && ($moduleName != 'xtmodule') && (!$this->_isFrontendExtension)) {
				$this->render('container-tree', 'left', true);
	    	}
		} else {
			throw new Exception('Error:Invalid parameter');
		}
	}


    /**
     * Die Default EDIT Action
     *
     */
    public function editAction()
    {
    	$request = $this->getRequest();
    	$table = $this->_getModelInstance();
		$primary = $table->getPrimary();
		$id = intval($this->_getParam($primary));

		// Rowobjekt instanzieren
		if($this->_request->isPost()) {
			$item = $table->createRow();
			$item->setFromArray($request->getParam($table->getClass()));
		} else {
			if($id > 0) {
				$select = $table->select();
				$select->where($primary . '= ?', $id);
				$item = $table->fetchRow($select);
			} else {
				$item = $table->createRow();
			}
		}

		// InstanceId aus der Session holen
		$session = $this->getHelper('Session');
        if($session->instance_id > 0) {
            $item->instance_id = $session->instance_id;
        }

		// Redirect (siehe viewAction)
		$redirect = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '' ;
		$redirect = $this->_getParam('redirect', $redirect);
		if(empty($redirect)) {
			$redirect = $session->redirect;
		} else {
			$session->redirect = $redirect;
		}

		// Javascript fuer onclick-Attribut in Button
		if(($this->_getParam('TEMPLATE') == 'popup') && (!$this->_isFrontendExtension)) {
			$onclickJs = 'window.close();';
		} else {
			$onclickJs = sprintf("javascript:window.location.href='%s';", $redirect);
		}

		$editor = $item->getFormEditor();
		$editor->setFormAction($_SERVER['REQUEST_URI']);
		$editor->addSubmit(WSB_BTN_SAVE, array('label' => 'button:save'));
		$editor->addSubmit(WSB_BTN_SAVE_CLOSE, array('label' => 'button:save/close'));
		$editor->addButton(WSB_BTN_CANCEL, array('label' => 'button:cancel', 'onclick' => $onclickJs));
		$editor->setRedirect($redirect);
		$form = $editor->getForm();
		if($this->_request->isPost() && (isset($_POST[WSB_BTN_SAVE]) || isset($_POST[WSB_BTN_SAVE_CLOSE])) && $form->isValid($this->_request->getPost())) {
			// Gefilterte Werte uebergeben
			$values = $form->getValues();
			unset($values['action']);
			$this->_forward('save', $this->_request->getControllerName(), $this->_request->getModuleName(), $values);
			return;
		} else {
			$this->view->form = $form;
			$this->view->tablename = $item->getTableName();
			$this->render('edit', null, true);

	    	// Page Tree rendern
	    	$moduleName = $request->getModuleName();
	    	if((substr($moduleName, 0, 2) == 'xt') && ($moduleName != 'xtmodule') && (!$this->_isFrontendExtension)) {
				$this->render('container-tree', 'left', true);
	    	}
		}

    }


	/**
	 * Die Default SAVE Action
	 *
	 */
	public function saveAction()
	{
		$this->_helper->viewRenderer->setNoRender();

		$request = $this->getRequest();
		if(!$request->isPost()) {
 			throw new Exception('Fehler in WSB_Controller_Action::saveAction(). Diese Action muss mittels POST aufgerufen werden.');
		}

		$redirect = $request->getPost('redirect');
		if(empty($redirect)) {
 			throw new Exception('Fehler in WSB_Controller_Action::saveAction(). POST-Parameter "redirect" fehlt.');
		}

    	if(($request->getPost(WSB_BTN_SAVE) != '') || ($request->getPost(WSB_BTN_SAVE_CLOSE) != '')) {

    		foreach($request->getPost() AS $key=>$values) {
				// Dies ist ein Model
				if(is_array($values) && substr($key, 0, 2) != '__') {
					$tmpTable = new $key();
					if($tmpTable->isIntersectionTable()) {
						reset($values);
						// Im Formular wurden keine Daten zugeordnet (bzw. ausgewaehlt)
						if(count($values) == 1) {
							if(is_numeric(current($values))) {
								$itemKey = key($values);
								$itemValue = current($values);
								$where = $tmpTable->getAdapter()->quoteInto("$itemKey = ?", $itemValue);
								$tmpTable->delete($where);
							}
						}
						// Im Formular wurden Daten zugeordnet (bzw. ausgewaehlt)
						if(count($values) == 2) {
							if(is_array(current($values))) {
								$arrayKey = key($values);
								$arrayValues = current($values);
								next($values);
								$itemKey = key($values);
								$itemValue = current($values);
							} else {
								$itemKey = key($values);
								$itemValue = current($values);
								next($values);
								$arrayKey = key($values);
								$arrayValues = current($values);
							}

							// Alle Rows loeschen mit $itemKey=$itemValue
							$where = $tmpTable->getAdapter()->quoteInto("$itemKey = ?", $itemValue);
							$tmpTable->delete($where);

							// Neue Rows schreiben
							foreach($arrayValues AS $value) {
								$data = array(
									$itemKey => $itemValue,
									$arrayKey => $value
								);
								$tmpTable->insert($data);
							}
						}
					}
				}
			}

			$table = $this->_getModelInstance();
    		$primary = $table->getPrimary();

			// POST Array
			$POST = $request->getPost($table->getClass());
			if(empty($POST[$primary])) {
				unset($POST[$primary]);
				$item = $table->createRow($POST);
			} else {
				$item = $table->find($POST[$primary])->current();
				$item->setFromArray($POST);
			}

			$item->save();

			// Sortierung
			if($table->hasColumn('sorting') && array_key_exists('sorting', $POST)) {
				$table->sortRowsAfterMoving($item);
			}

			WSB_Log::info(sprintf('Saved row %s with id %s', $item->getTableName(), $item->getPrimaryValue()));

			// Save and close
			if($request->getPost(WSB_BTN_SAVE_CLOSE) != '') {
                // Popup
				if(($this->_getParam('TEMPLATE') == 'popup') && (!$this->_isFrontendExtension)) {
			    //if($this->_getParam('TEMPLATE') == 'popup') {
                    $this->_response->appendBody('
                    <script type="text/javascript">
                        window.opener.location.reload();
                        window.close();
                    </script>
                    ');
                    return;
			    } else {
					$this->_helper->Redirect($redirect);
			    }
			// Save
			} else {
				$this->_helper->FlashMessenger->addMessage($this->_helper->Translate('entry was saved'));
				unset($_POST[WSB_BTN_SAVE]);
				$this->_forward('edit', null, null, array($table->getClass() => $item->toArray()));
				return;
			}
		}

		$this->editAction();
		//$redirector = $this->getHelper('Redirector');
		//$redirector->gotoSimple('edit');

	}

	/**
	 * The default delete action
	 *
	 */
    public function deleteAction()
	{
    	/*
    	$session = $this->getHelper('Session');
    	// Das Table-Objekt
		$table = $this->_getModelInstance();
		$primary = $table->getPrimary();
    	$id = $this->_getParam($primary);
    	$item = $table->find($id)->current();
    	$text = sprintf($this->_helper->Translate('delete.confirmation'), $id);
		$answer = WSB_Dialog::ConfirmDialog($text);
		if($answer == WSB_Dialog::YES_OPTION) {
			if($item->delete()) {
				if($table->hasColumn('sorting')) {
					$table->sortRowsAfterDeletion($item);
				}
				$this->_helper->FlashMessenger->addMessage(sprintf($this->_helper->Translate('delete.successfully'), $id));
				WSB_Log::info(sprintf('Deleted %s row with id %s', $table->getName(), $id));
			}
			if(!empty($session->postDeleteUrl)) {
			    $url = $session->postDeleteUrl;
			    unset($session->postDeleteUrl);
				$this->_helper->Redirect(str_replace(WSB_ADMIN_URL, '', $url));
			} else {
				$this->_helper->Redirect($this->getDefaultListUrl());
			}
		}
		if($answer == WSB_Dialog::NO_OPTION) {
			if(!empty($session->postDeleteUrl)) {
			    $url = $session->postDeleteUrl;
			    unset($session->postDeleteUrl);
				$this->_helper->Redirect(str_replace(WSB_ADMIN_URL, '', $url));
			} else {
				$this->_helper->Redirect($this->getDefaultListUrl());
			}
		}
		$this->_forward('list');
		*/
    	$session = $this->getHelper('Session');
    	// Das Table-Objekt
		$table = $this->_getModelInstance();
		$primary = $table->getPrimary();
    	$id = $this->_getParam($primary);
    	$item = $table->find($id)->current();
		if($item->delete()) {
			if($table->hasColumn('sorting')) {
				$table->sortRowsAfterDeletion($item);
			}
			$this->_helper->FlashMessenger->addMessage(sprintf($this->_helper->Translate('delete.successfully'), $id));
			WSB_Log::info(sprintf('Deleted %s row with id %s', $table->getName(), $id));
		}
		if(!empty($session->postDeleteUrl)) {
		    $url = $session->postDeleteUrl;
		    unset($session->postDeleteUrl);
			$this->_helper->Redirect(str_replace(WSB_ADMIN_URL, '', $url));
		} else {
			$this->_helper->Redirect($this->getDefaultListUrl());
		}
		$this->_forward('list');
    }

    public function hideAction()
	{
		$table = $this->_getModelInstance();
		$primary = $table->getPrimary();
    	$id = $this->_getParam($primary);
    	$item = $table->find($id)->current();
    	$item->hidden = 1;
    	$item->save();
		WSB_Log::info(sprintf('Hided row %s with id %s', $item->getTableName(), $id));
		$this->_forward('list');
    }

    public function unhideAction()
	{
		$table = $this->_getModelInstance();
		$primary = $table->getPrimary();
    	$id = $this->_getParam($primary);
    	$item = $table->find($id)->current();
    	$item->hidden = 0;
    	$item->save();
		WSB_Log::info(sprintf('Unhided row %s with id %s', $item->getTableName(), $id));
		$this->_forward('list');
    }

    /**
     * @param WSB_Db_Table $table
     * @param array $where
     * @return integer
     */
	protected function _getCountAll(WSB_Db_Table $table, array $where)
	{
		$info = $table->info();
		$db = $table->getAdapter();
		$select = $db->select();
		$select->from($info[WSB_Db_Table::NAME], array('count' => 'COUNT(*)'));
	    if(count($where) > 0) {
	    	foreach($where AS $key=>$value) {
	    		$select->where($key, $value);
	    	}
	    }
		$stmt = $select->query();
		$result = $stmt->fetchAll();
		if(is_array($result) && isset($result[0]['count'])) {
			return $result[0]['count'];
		}
		return 0;
	}

	/**
	 * @param array $filter
	 * @return array
	 */
	protected function _buildWhereStatement(array $filter)
	{
		$session = $this->getHelper('Session');
		$where = array();
		$where['deleted = ?'] = 0;
		if(!empty($session->instance_id)) {
			$where['instance_id=?'] = $session->instance_id;
		}
	    if(key_exists('key', $filter) && ($filter['key'] != '')) {
	        if(key_exists('value', $filter) && ($filter['value'] != '')) {
	        	$where[$filter['key'] . ' LIKE ?'] = '%'.$filter['value'].'%';
	        }
	    }
	    return $where;
	}

	/**
	 * @throws Exception
	 * @return integer
	 */
	protected function _getModelIndex()
	{
        $modelIndex = $this->_getParam('model', 0);
	    if(array_key_exists($modelIndex, $this->_models)) {
            return $modelIndex;
	    } else {
	        throw new Exception(sprintf('Fehler in WSB_Controller_Action::_getModelInstance(): Key %s nicht vorhanden in Array WSB_Controller_Action::$_models.', $modelIndex));
	    }
	}

	/**
	 * @return WSB_Db_Table
	 */
	protected function _getModelInstance()
	{
	    $modelIndex = $this->_getModelIndex();
        $class = $this->_models[$modelIndex];
        return new $class();
	}

	/**
	 * @param string $offset
	 * @param string $order
	 * @param integer $modelIndex
	 * @return string
	 */
    public function getDefaultListUrl($offset=null, $order=null, $modelIndex=0)
	{
    	$parameters = array();
    	if($modelIndex > 0) {
    		$parameters['model'] = $modelIndex;
    	}
    	if(isset($offset)) {
    		$parameters['offset'] = $offset;
    	}
    	if(!empty($order)) {
    		$parameters['order'] = $order;
    	}
    	$module = ($this->_request->getModuleName() == 'default') ? null : $this->_request->getModuleName();
    	return WSB_Controller_Url::getUrl($this->_request->getControllerName(), 'list', $parameters, $module);
    }

    /**
     * @param integer $modelIndex
     * @return string
     */
    public function getDefaultAddUrl($modelIndex=0)
	{
    	$parameters = array();
    	if($modelIndex > 0) {
    		$parameters['model'] = $modelIndex;
    	}
    	$module = ($this->_request->getModuleName() == 'default') ? null : $this->_request->getModuleName();
       	return WSB_Controller_Url::getUrl($this->_request->getControllerName(), 'edit', $parameters, $module);
    }

    /**
     * @param integer $id
     * @param integer $modelIndex
     * @return string
     */
    public function getDefaultViewUrl($id, $modelIndex=0)
	{
    	$parameters = array('id' => $id);
    	if($modelIndex > 0) {
    		$parameters['model'] = $modelIndex;
    	}
    	$module = ($this->_request->getModuleName() == 'default') ? null : $this->_request->getModuleName();
    	return WSB_Controller_Url::getUrl($this->_request->getControllerName(), 'view', $parameters, $module);
    }

    /**
     * @param integer $id
     * @param integer $modelIndex
     * @return string
     */
    public function getDefaultEditUrl($id, $modelIndex=0)
	{
    	$parameters = array('id' => $id);
    	if($modelIndex > 0) {
    		$parameters['model'] = $modelIndex;
    	}
    	$module = ($this->_request->getModuleName() == 'default') ? null : $this->_request->getModuleName();
    	return WSB_Controller_Url::getUrl($this->_request->getControllerName(), 'edit', $parameters, $module);
    }

    /**
     * @param integer $modelIndex
     * @return string
     */
    public function getDefaultSaveUrl($modelIndex=0)
	{
    	$parameters = array();
    	if($modelIndex > 0) {
    		$parameters['model'] = $modelIndex;
    	}
    	$module = ($this->_request->getModuleName() == 'default') ? null : $this->_request->getModuleName();
    	return WSB_Controller_Url::getUrl($this->_request->getControllerName(), 'save', $parameters, $module);
    }

    /**
     * @param integer $id
     * @param integer $modelIndex
     * @return string
     */
    public function getDefaultDeleteUrl($id, $modelIndex=0)
	{
    	$parameters = array('id' => $id);
    	if($modelIndex > 0) {
    		$parameters['model'] = $modelIndex;
    	}
    	$module = ($this->_request->getModuleName() == 'default') ? null : $this->_request->getModuleName();
    	return WSB_Controller_Url::getUrl($this->_request->getControllerName(), 'delete', $parameters, $module);
    }

    /**
     * @param integer $id
     * @param integer $modelIndex
     * @return string
     */
    public function getDefaultHideUrl($id, $modelIndex=0)
	{
    	$parameters = array('id' => $id);
    	if($modelIndex > 0) {
    		$parameters['model'] = $modelIndex;
    	}
    	$module = ($this->_request->getModuleName() == 'default') ? null : $this->_request->getModuleName();
    	return WSB_Controller_Url::getUrl($this->_request->getControllerName(), 'hide', $parameters, $module);
    }

    /**
     * @param integer $id
     * @param integer $modelIndex
     * @return string
     */
    public function getDefaultUnhideUrl($id, $modelIndex=0)
	{
    	$parameters = array('id' => $id);
    	if($modelIndex > 0) {
    		$parameters['model'] = $modelIndex;
    	}
    	$module = ($this->_request->getModuleName() == 'default') ? null : $this->_request->getModuleName();
    	return WSB_Controller_Url::getUrl($this->_request->getControllerName(), 'unhide', $parameters, $module);
    }

    /**
     * @param WSB_Db_Row $model
     * @param integer $modelIndex
     * @return array
     */
    public function buildListItemHtmlLinks(WSB_Db_Row $model, $modelIndex=0)
	{
    	$links = array();
    	array_push($links, $this->_buildEditListItemHtmlLink($model, $modelIndex));
    	array_push($links, $this->_buildDeleteListItemHtmlLink($model, $modelIndex));
    	if(isset($model->hidden)) {
    		array_push($links, $this->_buildHiddenListItemHtmlLink($model, $modelIndex));
    	}
    	return $links;
    }

    /**
     * @param WSB_Db_Row $model
     * @param integer $modelIndex
     * @return string
     */
    protected function _buildViewListItemHtmlLink(WSB_Db_Row $model, $modelIndex=0)
	{
    	return WSB_Controller_Url::getHtmlLink(WSB_Icon::view(), $this->getDefaultViewUrl($model->getPrimaryValue(), $modelIndex));
    }

    /**
     * @param WSB_Db_Row $model
     * @param integer $modelIndex
     * @return string
     */
    protected function _buildEditListItemHtmlLink(WSB_Db_Row $model, $modelIndex=0)
	{
		return WSB_Controller_Url::getHtmlLink(WSB_Icon::edit(), $this->getDefaultEditUrl($model->getPrimaryValue(), $modelIndex));
    }

    /**
     * @param WSB_Db_Row $model
     * @param integer $modelIndex
     * @return string
     */
    protected function _buildDeleteListItemHtmlLink(WSB_Db_Row $model, $modelIndex=0)
	{
    	$url = $this->getDefaultDeleteUrl($model->getPrimaryValue(), $modelIndex);
		return WSB_Controller_Url::getHtmlLink(WSB_Icon::delete(), "javascript:wsbYesNoDialog('".$url."', '".$model->getPrimaryValue()."');");
    	//return WSB_Controller_Url::getHtmlLink(WSB_Icon::delete(), $this->getDefaultDeleteUrl($model->getPrimaryValue(), $modelIndex));
    }

    /**
     * @param WSB_Db_Row $model
     * @param integer $modelIndex
     * @return string
     */
    protected function _buildHiddenListItemHtmlLink(WSB_Db_Row $model, $modelIndex=0)
	{
		if(empty($model->hidden)) {
			return WSB_Controller_Url::getHtmlLink(WSB_Icon::visible(), $this->getDefaultHideUrl($model->getPrimaryValue(), $modelIndex));
		} else {
			return WSB_Controller_Url::getHtmlLink(WSB_Icon::hidden(), $this->getDefaultUnhideUrl($model->getPrimaryValue(), $modelIndex));
		}
    }

}
